package com.ligongmusic.fragment;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ligongmusic.R;
import com.ligongmusic.activity.MyComment;
import com.ligongmusic.activity.MyPrise;
import com.ligongmusic.activity.MyUpload;
import com.ligongmusic.activity.UserInformation;
import com.ligongmusic.base.BaseFragment;
import com.ligongmusic.domain.Person;
import com.ligongmusic.util.ActivityManager;
import com.ligongmusic.util.GlobalParams;
import com.ligongmusic.util.JsonUtils;
import com.ligongmusic.util.LoggerUtils;
import com.ligongmusic.util.NetWorkUtils;
import com.ligongmusic.util.NetWorkUtils.NetWorkCallBack;
import com.ligongmusic.util.StringUtils;
import com.ligongmusic.view.RoundImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
@SuppressWarnings("deprecation")
public class Personal extends BaseFragment {
	private Person person;
	private RoundImageView user_head;
	private TextView tv_nick_name;
	private TextView tv_introduction;
	@Override
	protected View layoutInflater(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.personal_center, null);
	}

	@Override
	public void onResume() {
		super.onResume();
		getData();
	}

	@Override
	protected void getData() {
		LoggerUtils.getLog(Personal.class).error("update user info");
		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
		pairs.add(new BasicNameValuePair("token", GlobalParams.token));
		pairs.add(new BasicNameValuePair("loginname", GlobalParams.userName));
		NetWorkUtils.getRequest(pairs, GlobalParams.GET_USER_INFO, new NetWorkCallBack() {
			@Override
			public void success(String result) {
				Log.e("LoggerUtils", result);
				person = JsonUtils.jsonToBean(result, Person.class);
				if(person == null)
					return;
				GlobalParams.nickName = StringUtils.isEmpty(person.getNickname()) ? GlobalParams.userName : person.getNickname();
				GlobalParams.userImageUrl = StringUtils.isEmpty(person.getPhoto()) ? "" : person.getPhoto();
				ImageLoader.getInstance().displayImage(GlobalParams.IMAGE_URL + person.getPhoto(), user_head);
				if(!StringUtils.isEmpty(person.getIntroduction()))
					tv_introduction.setText(person.getIntroduction());
				else
					tv_introduction.setText("");
				tv_nick_name.setText(StringUtils.isEmpty(person.getNickname()) ? GlobalParams.userName : person.getNickname());
				// 加载完成后，设置为非首次加载
			}
			@Override
			public void error(String message) {
				// TODO Auto-generated method stub
			}
			@Override
			public void error() {
				// TODO Auto-generated method stub
			}
		});
	}
	
	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		if (isVisibleToUser) {
            //相当于Fragment的onResume
			getData();
        } else {
            //相当于Fragment的onPause
        }
	}

	@Override
	protected void initView() {
		// 个人信息
		((LinearLayout)getView(R.id.rl_info)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Bundle bundle = new Bundle();
				bundle.putSerializable("info", person);
				ActivityManager.startActivityForResult(getActivity(), UserInformation.class, bundle, 0x4);
			}
		});
		// 我的上传
		((LinearLayout)getView(R.id.my_upload)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityManager.startActivity(getActivity(), MyUpload.class);
			}
		});
		// 我的评论
		((LinearLayout)getView(R.id.my_comment)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Bundle bundle = new Bundle();
				bundle.putString("nickName", person == null ? "" : person.getNickname());
				ActivityManager.startActivity(getActivity(), MyComment.class, bundle);
			}
		});
		// 我的赞
		((LinearLayout)getView(R.id.my_prise)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Bundle bundle = new Bundle();
				bundle.putString("nickName", person == null ? "" : person.getNickname());
				ActivityManager.startActivity(getActivity(), MyPrise.class, bundle);
			}
		});
		// 用户头像
		user_head = getView(R.id.user_head);
		// 昵称
		tv_nick_name = getView(R.id.tv_nick_name);
		// 简介
		tv_introduction = getView(R.id.tv_introduction);
//		getData();
//		isFirst = true;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == 0x4 && resultCode == 0x1)
			getData();// 更新界面数据
		super.onActivityResult(requestCode, resultCode, data);
	}
	
}
